<template>
    <div>
        <h2>房型显示页面</h2>

        <input type="button" value="新增" class="btn btn-primary" @click="add">

        <div>
            房型名称：
            <input type="text" v-model="cc.typeName">
            <input type="button" value="查询" class="btn btn-primary" @click="cha">
        </div>

        <table class="table table-bordered">
            <tbody>
                <tr class="bg-primary">
                    <td>房型名称</td>
                    <td>单价（元）</td>
                    <td>室内面积（平方米）</td>
                    <td>最多入住人数</td>
                    <td>图片</td>
                    <td>房型描述</td>
                    <td>是否有窗</td>
                    <td>操作</td>
                </tr>

                <tr v-for="item in aa.list" :key="item.Id">
                    <td>{{ item.TypeName }}</td>
                    <td>{{ item.Price }}</td>
                    <td>{{ item.Area }}</td>
                    <td>{{ item.MaxCount }}</td>
                    <td>
                        <img :src="item.Photo" height="100" width="100" alt="">
                    </td>
                    <td>{{ item.Desc }}</td>
                    <td>{{ item.IsWin == 0 ? "是" : "否" }}</td>
                    <td>
                        <input type="button" value="修改" class="btn btn-warning" @click="upd(item.Id)">
                        <input type="button" value="删除" class="btn btn-danger" @click="del(item.Id)">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共 <b>{{ aa.ZongTiao }}</b> 条</span>
        <span>每页 <b>{{ cc.pageSize }}</b> 条</span>
        <span>共 <b>{{ cc.pageIndex }}/{{ aa.ZongYe }}</b> 页</span>

        <input type="button" value="首页" class="btn btn-info" @click="goPage(1)">
        <input type="button" value="下一页" class="btn btn-info" @click="goPage(cc.pageIndex + 1)">
        <span v-for="item in aa.ZongYe" :key="item" @click="goPage(item)">{{ item }}</span>
        <input type="button" value="上一页" class="btn btn-info" @click="goPage(cc.pageIndex - 1)">
        <input type="button" value="尾页" class="btn btn-info" @click="goPage(aa.ZongYe)">
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRoute, useRouter } from 'vue-router'
const router = useRouter();
const route = useRoute();
import moment from 'moment';

//--------修改跳转----------
const upd = (id: any) => {
    router.push({
        path: '/typeUpd',
        query: {
            id: id
        }
    })
}

//---------删除--------
const del = (id: any) => {
    if (confirm('确认删除吗')) {
        axios({
            method: 'delete',
            url: '/api/HouseType/DelType',
            params: {
                id: id
            }
        })
            .then(res => {
                if (res.data > 0) {
                    alert('成功');
                    getShow();
                }
                else {
                    alert('失败');
                }
            })
            .catch(err => {
                console.log(err);
            })
    }
}

//按钮
const goPage = (num: any) => {
    if (num < 1) {
        cc.pageIndex = 1;
        return;
    }
    if (num > aa.ZongYe) {
        cc.pageIndex = aa.ZongYe;
        return;
    }
    cc.pageIndex = num;
    getShow();
}

//分页查询
const cha = () => {
    cc.pageIndex = 1;
    getShow();
}

onMounted(() => {
    getShow();
})

let aa: any = reactive({
    ZongYe: 0,
    ZongTiao: 0,
    list: []
})
let cc: any = reactive({
    pageIndex: 1,
    pageSize: 2,
    typeName: ''
})

const getShow = () => {
    axios({
        method: 'get',
        url: '/api/HouseType/ShowType',
        params: cc
    })
        .then(res => {
            console.log(res);
            aa.ZongYe = res.data.ZongYe,
                aa.ZongTiao = res.data.ZongTiao,
                aa.list = res.data.list
        })
        .catch(err => {
            console.log(err);
        })
}

const add = () => {
    router.push('/typeAdd')
}
</script>

<style scoped>
span {
    margin-right: 10px;
}

b {
    color: red;
}

input[type="button"] {
    margin-right: 10px;
}
</style>